//
//  ViewController.m
//  知乎
//
//  Created by 王治亮 on 16/6/28.
//  Copyright © 2016年 王治亮. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeTableViewCell.h"
#import "LatestModel.h"
#import "StroriesModel.h"
#import "PicturesView.h"

#define CELLID @"HomeCell"

@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *datas;
@property (nonatomic,strong)PicturesView *picturesView;
@property (nonatomic,strong)LatestModel *latestModel;
@property (nonatomic,strong)UIView *headerView;

@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UIButton *leftBtn;
@end

@implementation HomeViewController

#pragma mark - 懒加载
-(UILabel *)titleLabel{
    if (!_titleLabel) {
        UILabel *label = [UILabel new];
        label.attributedText = [[NSAttributedString alloc] initWithString:@"今日要闻" attributes:@{
                                                                                               NSFontAttributeName:[UIFont systemFontOfSize:18],
                                                                                               NSForegroundColorAttributeName:[UIColor whiteColor]
                                                                                               }];
        [label sizeToFit];
        label.textAlignment = NSTextAlignmentCenter;
        _titleLabel = label;
    }
    return _titleLabel;
}
-(UIButton *)leftBtn{
    if (!_leftBtn) {
        _leftBtn = [[UIButton alloc]init];
        [_leftBtn addTarget:self action:@selector(didLeftMenubutton:) forControlEvents:UIControlEventTouchUpInside];
        [_leftBtn setImage:[UIImage imageNamed:@"Home_Icon"] forState:UIControlStateNormal];
        
    }
    return _leftBtn;
    
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[HomeTableViewCell class] forCellReuseIdentifier:CELLID];
        
        [_tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
        _tableView.tableHeaderView  =view;
    }
    return _tableView;
}
-(UIView *)headerView{
    if (!_headerView) {
        _headerView = [UIView new];
        _headerView.backgroundColor = [UIColor colorWithRed:23/255.0 green:150/255.0 blue:210/255.0 alpha:0];
        _headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 56);
        [_headerView addSubview:self.leftBtn];
        [_headerView addSubview:self.titleLabel];
        
        
        self.titleLabel.sd_layout.centerYEqualToView(_headerView).centerXEqualToView(_headerView).widthIs(150).heightIs(30);
        self.leftBtn.sd_layout.topSpaceToView(_headerView,13).leftSpaceToView(_headerView,12).widthIs(30).heightIs(30);
    }
    
    return _headerView;
}
-(PicturesView *)picturesView{
    if (!_picturesView) {
        _picturesView = [[PicturesView alloc]init];
        _picturesView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 240);
    }
    return _picturesView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.picturesView];
    [self.view addSubview:self.headerView];
//    self.picturesView.sd_layout.topSpaceToView(self.view,0).leftSpaceToView(self.view,0).rightSpaceToView(self.view,0).heightIs(220+20);
    
    [self getLatest];
}
#pragma mark - 获取新闻数据
-(void)getLatest{
    [[AFHTTPSessionManager manager] GET:@"http://news-at.zhihu.com/api/4/news/latest" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [LatestModel mj_setupObjectClassInArray:^NSDictionary *{
            return @{
                     @"stories":@"StroriesModel",
                     @"top_stories":@"PicturesModel"
                     };
        }];
        LatestModel *model = [LatestModel mj_objectWithKeyValues:responseObject];
        self.datas=model.stories.mutableCopy;
        self.latestModel = model;
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
#pragma mark - UITableView Delegate & DataSource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self.tableView cellHeightForIndexPath:indexPath model:self.datas[indexPath.row] keyPath:@"model" cellClass:[HomeTableViewCell class] contentViewWidth:self.view.frame.size.width];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID];
    cell.model = self.datas[indexPath.row];
    return cell;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"contentOffset"]) {
        CGFloat yOffset = self.tableView.contentOffset.y;
        
        if (yOffset<= 0) {
            self.picturesView.height = 220-yOffset;
            CGRect frame = self.picturesView.frame;
            frame.origin.y = 0;
            self.picturesView.frame = frame;
        }else{
            CGRect frame = self.picturesView.frame;
            frame.origin.y = 0-yOffset;
            self.picturesView.frame = frame;
        }
        CGFloat alpha = 0;
        if (yOffset < 100) {
            alpha = 0;
        }else if (yOffset<165){
            alpha = (yOffset- 100)/(165-100);
        }else{
            alpha = 1;
        }
        NSLog(@"%f",yOffset);
        _headerView.backgroundColor = [UIColor colorWithRed:23/255.0 green:150/255.0 blue:210/255.0 alpha:alpha];
    }
}
-(void)setDatas:(NSMutableArray *)datas{
    _datas = datas;
    [self.tableView reloadData];
}
-(void)setLatestModel:(LatestModel *)latestModel{
    _latestModel = latestModel;
    self.picturesView.images = latestModel.top_stories;
}
-(void)didLeftMenubutton:(UIButton *)btn{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
